package com.sym.ffmpeg;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.sym.ffmpeg.databinding.ActivityMainBinding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(mBinding.getRoot());

        // Example of a call to a native method


        initView();
    }

    private void initView() {

        addButton("show FFmpeg version", v -> {
            getFfmpegVersion();
        });

        addButton("show build configure", v -> {
            showBuildConfigure();
        });

        addButton("extract aac from video", v -> {
            File srcFile = copyFile();
            if (srcFile != null) {
                File dstFile = new File(getCacheDir().getAbsoluteFile(), "test.aac");
                if (dstFile.exists()) {
                    dstFile.delete();
                }
                extractAacFromVideo("file:" + srcFile.getAbsolutePath(), "file:" + dstFile.getAbsolutePath());
            }
        });

        addButton("play aac file", v -> {
            File dstFile = new File(getCacheDir().getAbsoluteFile(), "test.aac");
            ReadAACFileThread readAACFileThread = new ReadAACFileThread(dstFile.getAbsolutePath());
            readAACFileThread.start();
        });

        addButton("extract h264 from video", v -> {

            File srcFile = copyFile();
            if (null != srcFile) {
                File destFile = new File(getCacheDir().getAbsoluteFile(), "sintel.h264");
                if (destFile.exists()) {
                    destFile.delete();
                }
                extractVideo("file:" + srcFile.getAbsolutePath(), "file:" + destFile.getAbsolutePath());
            }
        });

        addButton("play h264", v -> {

            Intent intent = new Intent(MainActivity.this, ParseH264FileActivity.class);
            File destFile = new File(getCacheDir().getAbsoluteFile(), "sintel.h264");
            intent.putExtra("file", destFile.getAbsolutePath());
            startActivity(intent);
        });
    }


    private native void extractVideo(String filePath, String dstFilePath);

    private native void extractAacFromVideo(String srcPath, String dstPath);


    private native void init();

    private native void showBuildConfigure();

    private native void getFfmpegVersion();

    /**
     * 将asset目录中的媒体文件拷贝到sd卡中
     */
    File copyFile() {
        String fileName = "sintel.mp4";

        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //将asset中的文件复制到sd卡中
            inputStream = getAssets().open(fileName);
            File file = new File(getCacheDir().getAbsolutePath(), fileName);
            if (file.exists()) {
                file.delete();
            }
            fileOutputStream = new FileOutputStream(file);
            byte[] buffer = new byte[8192];
            int byteCount;
            while ((byteCount = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, byteCount);
            }
            fileOutputStream.flush();

            return file;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            return null;
        } finally {
            try {
                if (inputStream != null) inputStream.close();
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void addButton(String name, View.OnClickListener lisener) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(20, 20, 20, 20);

        Button button = new Button(this);
        button.setPadding(0, 20, 0, 20);
        button.setGravity(Gravity.CENTER);
        button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        button.setText(name);
        button.setAllCaps(false);
        button.setOnClickListener(lisener);

        mBinding.container.addView(button, lp);
    }

}